package com.njxzc.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.common.http.ResponseResult;
import com.njxzc.entity.GoodsTypeEntity;
import com.njxzc.mapper.admin.GoodsTypeMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper, GoodsTypeEntity> implements GoodsTypeService {

    @Override
    public ResponseResult<Map<String, Object>> addGoodsType(GoodsTypeEntity goodsTypeEntity) {
        /**
         * *
         */
        boolean res = save(goodsTypeEntity);
        if(res){
            return ResponseResult.getMessageResult(null,"A001");
        }

        return ResponseResult.getMessageResult(null,"A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> updateGoodsType(GoodsTypeEntity goodsTypeEntity) {
        if(updateById(goodsTypeEntity)){
            return ResponseResult.getMessageResult(null,"A001");
        }
        return ResponseResult.getMessageResult(null,"A002");
    }

    @Override
    public ResponseResult<Map<String, Object>> deleteGoodsType(GoodsTypeEntity goodsTypeEntity) {
        //TODO
        long res=0;
        if(res >0){
            return ResponseResult.getMessageResult(null,"A001");
        }
       if(removeById(goodsTypeEntity.getId())){
           return ResponseResult.getMessageResult(null,"A002");
       }
       return ResponseResult.getMessageResult(null,"A003");
        /**
         * *
         */
    }

    @Override
    public ResponseResult<Map<String, Object>> getGoodsType(GoodsTypeEntity goodsTypeEntity) {
        Integer currentPage = goodsTypeEntity.getCurrentPage();
        if(currentPage == null||currentPage<0){
            currentPage=1;
        }
        /**
         page(iPage)方法是MyBatis-Plus提供的用于执行分页查询的方法。它接受一个实现了
         IPage接口的对象作为参数，并根据这个对象中的分页信息（如当前页和每页大小）来查询
         数据库。查询结果同样是一个实现了IPage接口的对象，这里将其赋值给变量page。这个
         page对象包含了查询到的数据列表（可以通过getRecords()方法获取），以及分页信息，
         比如总记录数、总页数等
         QueryWrapper<GoodsTypeEntity> queryWrapper = new QueryWrapper<>();
         可以为查询条件设置一些过滤规则
         queryWrapper.eq("column_name", value);
         return goodsTypeMapper.selectPage(page, queryWrapper);
         */
        IPage<GoodsTypeEntity> ipage = new Page<>(goodsTypeEntity.getCurrentPage(),5);
        IPage<GoodsTypeEntity> page = this.page(ipage);
        Map<String, Object> npapresult = new HashMap<>();
        npapresult.put("allGoodsType",page.getPages());
        npapresult.put("totalPage",page.getPages());
        return ResponseResult.getSuccessResult(npapresult);
    }
}
